home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 02 / wintro5 / tick.c < prev   
C/C++ Source or Header  |  1991-02-28  |  6KB  |  179 lines

  1. /*==================================================================*/
  2. /*                                                                  */
  3. /* File    : TICK.C                                                 */
  4. /*                                                                  */
  5. /* Purpose : Routines pertaining to the adding and modification of  */
  6. /*           tickers.                                               */
  7. /*                                                                  */
  8. /* History :                                                        */
  9. /*                                                                  */
  10. /* Written by Marc Adler/Magma Systems for Microsoft Systems Journal*/
  11. /*==================================================================*/
  12.  
  13. #include <windows.h>
  14. #include "stock.h"
  15.  
  16.  
  17. /*******************************************************************/
  18. /*                                                                 */
  19. /* Function : AddTickDlgProc()                                     */
  20. /*                                                                 */
  21. /* Purpose  : Dialog box proc for adding tickers.                  */
  22. /*                                                                 */
  23. /* Returns  :                                                      */
  24. /*                                                                 */
  25. /*******************************************************************/
  26. BOOL FAR PASCAL AddTickDlgProc(hDlg, msg, wParam, lParam)
  27.   HWND hDlg;
  28.   WORD msg;
  29.   WORD wParam;
  30.   LONG lParam;
  31. {
  32.   RECT        r;
  33.   char        buf[80];
  34.   LPSTOCKINFO lpStockInfo;
  35.   LPTICK      lpTick;
  36.   TICK        tick;
  37.   HWND        hWnd;
  38.  
  39.   static HANDLE hStockInfo;
  40.  
  41.  
  42.   switch (msg)
  43.   {
  44.     case WM_INITDIALOG:
  45.       /*
  46.          Make sure that there is a current stock information record
  47.          which we can append the ticker onto.
  48.       */
  49.       if (!hCurrStockInfo)
  50.         EndDialog(hDlg, FALSE);
  51.       return TRUE;
  52.  
  53.  
  54.     case WM_COMMAND:
  55.       switch (wParam)
  56.       {
  57.         /*
  58.           The user chose the OK button...
  59.         */
  60.         case IDOK:
  61.           /*
  62.             Get a pointer to the stock info record
  63.           */
  64.           lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo);
  65.           if (lpStockInfo == NULL)
  66.           {
  67.             MessageBox(hDlg, "GlobalLock returned NULL", 
  68.                              "Error", MB_OK);
  69.             goto byebye;
  70.           }
  71.  
  72.           /*
  73.             Are we entering the first ticker? If so, then allocate a
  74.             ticker array for the stock. We allocate 64 tickers
  75.             initially.
  76.           */
  77.           if (lpStockInfo->hTicks == NULL)
  78.           {
  79.             lpStockInfo->StockFile.nTicks = 0;
  80.             lpStockInfo->nTicksAllocated  = 64;
  81.             lpStockInfo->hTicks = GlobalAlloc(GMEM_MOVEABLE,
  82.                                        (DWORD) sizeof(TICK) * 64);
  83.             if (lpStockInfo->hTicks == NULL)
  84.             {
  85.               MessageBox(hDlg, "Can't allocate initial ticks",
  86.                                "Error", MB_OK);
  87.               goto byebye;
  88.             }
  89.           }
  90.  
  91.           /*
  92.             Make sure that we do not overflow the ticker array. If
  93.             there is a chance of this, then reallocate.
  94.           */
  95.           if (lpStockInfo->StockFile.nTicks + 1 >= 
  96.                                      lpStockInfo->nTicksAllocated)
  97.           {
  98.             HANDLE h;
  99.             lpStockInfo->nTicksAllocated *= 2;
  100.             h = GlobalReAlloc(lpStockInfo->hTicks,
  101.                               (DWORD) sizeof(TICK) *
  102.                                       lpStockInfo->nTicksAllocated,
  103.                               GMEM_MOVEABLE);
  104.             if (h == NULL)
  105.             {
  106.               MessageBox(hDlg,
  107.                  "Could not allocate enough memory for the tickers",
  108.                               "Error", MB_OK);
  109.               goto byebye;
  110.             }
  111.             else
  112.               lpStockInfo->hTicks = h;
  113.           }
  114.  
  115.           /*
  116.             Get a pointer to the ticker array
  117.           */
  118.           if (!(lpTick = (LPTICK) GlobalLock(lpStockInfo->hTicks)))
  119.           {
  120.             MessageBox(hDlg, "GlobalLock returned NULL", 
  121.                              "Error", MB_OK);
  122.             GlobalUnlock(hCurrStockInfo);
  123.             goto byebye;
  124.           }
  125.  
  126.           /*
  127.             Get the price and volume and out it in the last element
  128.             of the ticker array.
  129.             KLUDGE NOTES :
  130.               a) We really should sort the records by date...
  131.               b) We ignore the date here.
  132.           */
  133.           tick.price = GetDlgItemLong(hDlg, ID_TICK_PRICE, 
  134.                                       NULL, FALSE);
  135.           tick.dwVolume = GetDlgItemLong(hDlg, ID_TICK_VOLUME,
  136.                                       NULL, FALSE);
  137.  
  138.           /*
  139.             Copy the ticker structure
  140.           */
  141.           lpTick[lpStockInfo->StockFile.nTicks++] = tick;
  142.           lpStockInfo->dwFlags |= STATE_DIRTY;
  143.  
  144.           /*
  145.             Unlock the memory and get outta here...
  146.           */
  147.           GlobalUnlock(lpStockInfo->hTicks);
  148.           GlobalUnlock(hCurrStockInfo);
  149.           EndDialog(hDlg, TRUE);
  150.           break;
  151.  
  152.  
  153.         /*
  154.           The user chose the CANCEL button....
  155.         */
  156.         case IDCANCEL :
  157. byebye:
  158.           EndDialog(hDlg, FALSE);
  159.           break;
  160.       }
  161.       return TRUE;
  162.  
  163.     default:
  164.       return FALSE;
  165.   }
  166. }
  167.  
  168.  
  169. LONG GetDlgItemLong(HWND hDlg, WORD id, BOOL FAR *lpTranslated,
  170.                     BOOL bSigned)
  171. {
  172.   extern long atol();
  173.   char szBuf[64];
  174.  
  175.   GetDlgItemText(hDlg, id, (LPSTR) szBuf, sizeof(szBuf));
  176.   return atol(szBuf);
  177. }
  178.  
  179.